package action;
//----------------------8888lk99990000099------------------------
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

import entity.Keyword;



import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;





public class main {
	private static String arg0="entity/Keyword.hbm.xml";
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String sourcefile="g:\\海关数据\\201610.xls";
		String code="",pice="",year="",mouth="10",country="",money="",amout="";
		long codeid =0 ;
		int number=5052188,times=3;
		String saveurl="g:\\海关数据\\201610.txt";
		int type=2;


	 	List<String > set=new ArrayList<String>();
	 	set.add("141　越南");
	 	set.add("136　泰国");
	 	set.add("112　印度尼西亚");
	 	set.add("122　马来西亚");
	 	set.add("129　菲律宾");
	 	set.add("132　新加坡");
	 	set.add("106　缅甸");
	 	set.add("107　柬埔寨");
	 	set.add("105　文莱");
	 	set.add("119　老挝");
	 	Map<String,Integer> map=new HashMap<String, Integer>();
	 	map.put("141　越南",141);
	 	map.put("136　泰国",136);
	 	map.put("112　印度尼西亚",112);
	 	map.put("122　马来西亚",122);
	 	map.put("129　菲律宾",129);
	 	map.put("132　新加坡",132);
	 	map.put("106　缅甸",106);
	 	map.put("107　柬埔寨",107);
	 	map.put("105　文莱",105);
	 	map.put("119　老挝",119);


		mapaa mapa=new mapaa();



		try {
			InputStream is = new FileInputStream(sourcefile);

			Workbook workbook = Workbook.getWorkbook(is);

			System.out.println(workbook.getNumberOfSheets());
			for(int i=0;i<times;i++){
		//	for(int i=3;i<6;i++){
				if(type==2)
					type=0;
				else
					type++;

			Sheet rs = workbook.getSheet(i);
			for(int j=2;j<rs.getRows();j++){

			Cell c00 = rs.getCell(0, j);
			String strc00 = c00.getContents();

			Cell c01 = rs.getCell(1, j);
			String strc01 = c01.getContents();
			Cell c02 = rs.getCell(2, j);
			String strc02 = c02.getContents();

			Cell c03 = rs.getCell(3, j);
			String strc03 = c03.getContents();
			Cell c04 = rs.getCell(4, j);
			String strc04 = c04.getContents();
			Cell c05 = rs.getCell(5, j);
			String strc05 = c05.getContents();

			Cell c06 = rs.getCell(6, j);
			String strc06 = c06.getContents();

			if(strc03 !=""){



				if(j!=2){

					for(int k=0;k<set.size();k++){

						try {
				    		FileWriter fileWriter=new FileWriter(saveurl, true);
				    		number++;
				    		fileWriter.write(number+","+codeid+","+pice+","+year+","+mouth+","+map.get(set.get(k))+","+0+","+0+","+type);
				    		fileWriter.write("\r\n");
				    		fileWriter.close();
				    	} catch (IOException e) {
				    		// TODO Auto-generated catch block
				    		e.printStackTrace();
				    	} // true代表追加

						System.out.println(number+","+codeid+","+pice+","+year+","+mouth+","+map.get(set.get(k))+","+0+","+0+","+type);
					}
					mouth=strc03;
				}
			if(strc00 !=""){
				code=strc00;
			codeid=getcode(code);}
			if(strc01 !="")

				pice=strc01;
			if(strc02 !="")
				year=strc02;


			  set=new ArrayList<String>();
			 	set.add("141　越南");
			 	set.add("136　泰国");
			 	set.add("112　印度尼西亚");
			 	set.add("122　马来西亚");
			 	set.add("129　菲律宾");
			 	set.add("132　新加坡");
			 	set.add("106　缅甸");
			 	set.add("107　柬埔寨");
			 	set.add("105　文莱");
			 	set.add("119　老挝");

				}
			if(strc04 !=""){
				country=strc04;
				set.remove(country);
			}

			if(strc05 !="")
				money=strc05;
			if(strc06 !="")
				amout=strc06;
			try {
	    		FileWriter fileWriter=new FileWriter(saveurl, true);
	    		number++;
	    		fileWriter.write(number+","+codeid+","+pice+","+year+","+mouth+","+map.get(country)+","+money+","+amout+","+type);
	    		fileWriter.write("\r\n");
	    		fileWriter.close();
	    	} catch (IOException e) {
	    		// TODO Auto-generated catch block
	    		e.printStackTrace();
	    	} // true代表追加

			System.out.println(number+","+codeid+","+pice+","+year+","+mouth+","+map.get(country)+","+money+","+amout+","+type);

			}



			for(int k=0;k<set.size();k++){
				try {
			 		codeid=getcode(code);
		    		FileWriter fileWriter=new FileWriter(saveurl, true);
		    		number++;
		    		fileWriter.write(number+","+codeid+","+pice+","+year+","+mouth+","+map.get(set.get(k))+","+0+","+0+","+type);
		    		fileWriter.write("\r\n");
		    		fileWriter.close();
		    	} catch (IOException e) {
		    		// TODO Auto-generated catch block
		    		e.printStackTrace();
		    	} //
				System.out.println(number+","+codeid+","+pice+","+year+","+mouth+","+map.get(set.get(k))+","+0+","+0+","+type);
			}


			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 catch (BiffException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}



	}

	static long getcode(String codename){
		long a = 0;
		 Configuration c1=new Configuration();
		 c1.addResource(arg0);
		 SessionFactory session=c1.buildSessionFactory();
			Pattern pattern=Pattern .compile("\\d*");
			Matcher matcher=pattern.matcher(codename);
			matcher.find();
			codename=codename.substring(matcher.start(),matcher.end());
			System.out.println(codename);
		 Session s=session.openSession();
		 Transaction t=s.beginTransaction();
		 Criteria c=s.createCriteria(Keyword.class);

		   c.add(Restrictions.eq("code",codename));//eq是等于，gt是大于，lt是小于,or是或
		   List<Keyword> code=c.list();
		   if(code.size()!=0){
			   a=code.get(0).getGoodscode();
		   }

			 t.commit();
			 s.close();
			 session.close();

		return a;

	}




}
